package 课工场练习;

public class Operation {
	public Vehicle[] vehicle = new Vehicle[8];

	// 初始化汽车信息
	public void init() {
		vehicle[0] = new Car("京NY28588", "宝马", 800, "X6"); // vehicle v = new Car();
		vehicle[1] = new Car("京CNY32584", "宝马", 600, "550i"); // vehicle v = new Car();
		vehicle[2] = new Car("京NT37465", "别克", 300, "林荫大道"); // vehicle v = new Car();
		vehicle[3] = new Car("京NT96968", "别克", 600, "GL8"); // vehicle v = new Car();
		vehicle[4] = new Bus("京6566754", "金杯", 800, 16); // vehicle v = new Bus();
		vehicle[5] = new Bus("京8696997", "金龙", 800, 16); // vehicle v = new Bus();
		vehicle[6] = new Bus("京9696996", "金杯", 1500, 34); // vehicle v = new Bus();
		vehicle[7] = new Bus("京8696998", "金龙", 1500, 34); // vehicle v = new Bus();
	}

	// 租车：根据用户提供的条件去汽车数组中查找相应车辆并返回
	// 如果租赁的是轿车 需要条件：品牌 型号
	// 如果租赁的是客车 需要条件：品牌 座位数
	// 简单工厂模式
	public Vehicle zuChe(String brand, String type, int seatCount) {
		Vehicle che = null;
		// for循环遍历数组vehicle
		for (Vehicle myche : vehicle) {
			// 判断Vehicle类的myChe的类型是否和Car一样
			if (myche instanceof Car) {
				// Vehicle类的myChe向下转型变成子类Car
				Car car = (Car) myche;
				if (car.getBrand().equals(brand) && car.getType().equals(type)) {
					che = car;
					break;
				}
			} else {
				// Vehicle类的myChe向下转型变成子类Bus
				Bus bus = (Bus) myche;
				if (bus.getBrand().equals(brand) && bus.getSeatCount() == seatCount) {
					che = bus;
					break;
				}
			}
		}
		return che;

	}
}
